package com.douqu.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.mergeManager.mergeUtils.DatabaseConfig;
import com.douqu.game.mergeManager.mergeUtils.DatabaseFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-08-16 10:18
 */
public class FixManager {

    public static void main(String[] args)
    {
        String host = "127.0.0.1";
        String user = "root";
        String pwd = "123456";
        String db = "pet";
//        mergePet(host, db, user, pwd);
//        deleteOld(host, db, user, pwd);
//        fixOther(host, db, user, pwd);
        testPet(host, db, user, pwd);
    }



    private static void testPet(String host, String db, String user, String pwd)
    {
        DatabaseFactory databaseFactory = new DatabaseFactory(new DatabaseConfig(host, "3306", db, user ,pwd));
        List<Map<String, Object>> list = databaseFactory.query("select * from role_pet where role_id = 26814");

        String petStr = list.get(0).get("pets").toString();
        Map<Integer, RolePet> pets = JSON.parseObject(petStr,new TypeReference<HashMap<Integer,RolePet>>(){});
        for(Map.Entry<Integer, RolePet> entry : pets.entrySet())
        {
            System.out.println("id:" + entry.getValue().getPetId() + "  i:" + entry.getKey());
        }
    }

    private static void fixOther(String host, String db, String user, String pwd)
    {
        DatabaseFactory databaseFactory = new DatabaseFactory(new DatabaseConfig(host, "3306", db, user ,pwd));
        List<Map<String, Object>> list = databaseFactory.query("select * from role_info");
        System.out.println("role_info -> " + list.size());
        List<Long> infoIdList = new ArrayList<>();
        for(Map<String, Object> map : list)
        {
            infoIdList.add((Long) map.get("id"));
        }

        fixOther(databaseFactory, "role_battle", infoIdList);

        fixOther(databaseFactory, "role_boon", infoIdList);

        fixOther(databaseFactory, "role_challenge", infoIdList);

        fixOther(databaseFactory, "role_currency", infoIdList);

        fixOther(databaseFactory, "role_pet", infoIdList);
    }

    private static void fixOther(DatabaseFactory databaseFactory, String tableName, List<Long> infoIdList)
    {
        List<Map<String, Object>> list = databaseFactory.query("select * from " + tableName);
        List<Long> roleIdList = new ArrayList<>();
        System.out.println(tableName + " -> " + list.size());
        for(Map<String, Object> map : list)
        {
            roleIdList.add((Long) map.get("role_id"));
        }

        List<Long> list1 = new ArrayList<>(infoIdList);
        List<Long> list2 = new ArrayList<>(roleIdList);
        list1.removeAll(list2);
        //要添加数据
        if(!list1.isEmpty())
        {
            System.out.println("要添加 -> " + tableName + " id -> " + list1);
            String sql = getAddSql(tableName, list1);
            System.out.println("sql -> " + sql);
            int count = databaseFactory.execute(sql);
            System.out.println("执行添加成功,count -> " + count);
        }


        List<Long> list3 = new ArrayList<>(infoIdList);
        List<Long> list4 = new ArrayList<>(roleIdList);
        list4.removeAll(list3);
        //要删除role_info表里的数据
        if(!list4.isEmpty())
        {
            System.out.println("要删除 -> role_info id -> " + list4);
            String sql = getDelSql(tableName, list4, "role_id");
            System.out.println("sql -> " + sql);
            int count = databaseFactory.execute(sql);
            System.out.println("执行删除成功,count -> " + count);
        }

    }

    private static String getDelSql(String tableName, List<Long> ids, String key)
    {
        StringBuffer result = new StringBuffer();
        result.append("delete from ");
        result.append(tableName);
        result.append(" where ");
        result.append(key);
        result.append(" in (");
        for(long id : ids)
        {
            result.append(id);
            result.append(",");
        }
        return result.substring(0, result.length()-1) + ")";
    }

    private static String getAddSql(String tableName, List<Long> ids)
    {

        StringBuffer result = new StringBuffer();
        result.append("insert into ");
        result.append(tableName);
        switch (tableName)
        {
            case "role_battle":
                result.append(" (role_id,addition,battle_time,fight,stage_id) ");
                break;
            case "role_boon":
                result.append(" (role_id,invitation_roles,invitation_times,rewards,share_times,good_share_times,last_sign_date,sign_times) ");
                break;
            case "role_challenge":
                result.append(" (role_id,friends) ");
                break;
            case "role_currency":
                result.append(" (role_id,diamond,gold) ");
                break;
            case "role_pet":
                result.append(" (role_id,gold_times,pets,diamonds_times) ");
                break;
        }
        result.append("VALUES ");
        StringBuffer buffer = null;
        for(long id : ids)
        {
            result.append("(");
            result.append(id);
            switch (tableName)
            {
                case "role_battle":
                    result.append(",'[]',0,0,1");
                    break;
                case "role_boon":
                    result.append(",'[]',0,'[]',0,0,0,0");
                    break;
                case "role_challenge":
                    result.append(",'[]'");
                    break;
                case "role_currency":
                    result.append(",0,0");
                    break;
                case "role_pet":
                    result.append(",0,'");
                    result.append(JSONObject.toJSONString(new HashMap<>()));
                    result.append("',0");
                    break;
            }
            result.append("),");
        }


        return result.substring(0, result.length()-1);

    }


    private static void deleteOld(String host, String db, String user, String pwd)
    {
        DatabaseFactory databaseFactory = new DatabaseFactory(new DatabaseConfig(host, "3306", db, user ,pwd));
        List<Map<String, Object>> infoList = databaseFactory.query("select * from role_info");
        StringBuffer buffer = new StringBuffer();
        for(Map<String, Object> map : infoList)
        {
            buffer.append(map.get("id"));
            buffer.append(",");
        }
        String sql = "delete from role_info_old where id in (" + buffer.substring(0, buffer.length()-1) + ")";
        int count = databaseFactory.execute(sql);
        System.out.println("delete count -> " + count);
    }

    private static void mergePet(String host, String db, String user, String pwd)
    {
        DatabaseFactory databaseFactory = new DatabaseFactory(new DatabaseConfig(host, "3306", db, user ,pwd));
        List<Map<String, Object>> infoList = databaseFactory.query("select a.id,a.account,r.exp,r.level,r.head,r.nick_name,r.step_id,a.parent_id,a.platform,r.create_time " +
                "from account a, role_info_old r where a.id = r.id");
        System.out.println("infoList -> " + infoList);

        String sql = "INSERT INTO role_info (id,account,exp,level,head,nick_name,step_id,parent_id,platform,create_time) VALUES ";
        StringBuffer sb = new StringBuffer();
        for(Map<String, Object> map : infoList)
        {
            StringBuffer buffer = new StringBuffer();
            buffer.append("(");
            buffer.append(map.get("id"));
            buffer.append(",'");
            buffer.append(map.get("account"));
            buffer.append("',");
            buffer.append(map.get("exp"));
            buffer.append(",");
            buffer.append(map.get("level"));
            buffer.append(",'");
            buffer.append(map.get("head"));
            buffer.append("','");
            buffer.append(map.get("nick_name"));
            buffer.append("',");
            buffer.append(map.get("step_id"));
            buffer.append(",");
            buffer.append(map.get("parent_id"));
            buffer.append(",");
            buffer.append(map.get("platform"));
            buffer.append(",'");
            Object createTime = map.get("create_time");
            if(createTime != null)
            {
                buffer.append(createTime);
            }
            else
            {
                buffer.append(TimeUtils.getCurrentTime());
            }
            buffer.append("')");

            sb.append(buffer.toString());
            sb.append(",");
        }

        sql += sb.substring(0, sb.length()-1);

        System.out.println("sql -> " + sql);

        int count = databaseFactory.execute(sql);
        System.out.println("copy 执行完成 -> count:" + count);
    }

    private static void fixWorld()
    {
        DatabaseConfig databaseConfig = new DatabaseConfig("127.0.0.1", "3306", "slg2001", "root", "123456");
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM server");

        Map<String, Object> data = dataList.get(0);
        byte[] worldByte = (byte[]) data.get("worldInfo");
        LogUtils.info("worldInfo -> " + worldByte.length);

        WorldInfoFactory worldInfoFactory = WorldInfoFactory.getInstance();
        worldInfoFactory.loadWorldInfo(worldByte);

        LogUtils.info("加载WorldInfo成功 -> ");

//        byte[] playerMapByte = (byte[]) data.get("playerWorldMapInfo");
//        LogUtils.info("playerWorldMapInfo -> " + playerMapByte.length);
    }
}


class RolePet {
    public RolePet() {
    }

    private int petId;
    private int star;
    private long hp;
    private long atk;
    private long spd;
    private long fightValue;
    //private Map<Integer, Integer> composePet = new HashMap<>();

    public int getPetId() {
        return petId;
    }

    public void setPetId(int petId) {
        this.petId = petId;
    }

    public int getStar() {
        return star;
    }

    public void setStar(int star) {
        this.star = star;
    }

    public long getHp() {
        return hp;
    }

    public void setHp(long hp) {
        this.hp = hp;
    }

    public long getAtk() {
        return atk;
    }

    public void setAtk(long atk) {
        this.atk = atk;
    }

    public long getSpd() {
        return spd;
    }

    public void setSpd(long spd) {
        this.spd = spd;
    }

    public long getFightValue() {
        return fightValue;
    }

    public void setFightValue(long fightValue) {
        this.fightValue = fightValue;
    }
}